<template>
  <div class="home">
    <el-container>
      <el-header>
        <div
          @click="
            $route.path === '/welcome' ? '' : $router.push('/welcome'),
              closeMenu()
          "
        >
          <img src="../assets/logo.png" alt="" />
          <span>电商后台管理系统</span>
        </div>
        <el-button @click="logout" type="info">退出</el-button>
      </el-header>
      <el-container>
        <el-aside :width="isCollapse ? 'auto' : '200px'">
          <el-menu
            :collapse="isCollapse"
            :collapse-transition="false"
            :default-active="currentHash"
            class="el-menu-vertical-demo"
            background-color="#333744"
            text-color="#fff"
            unique-opened
            router
            active-text-color="rgb(64, 158, 255)"
            ref="menu"
          >
            <!-- hash -->
            <!-- 是否水平折叠收起菜单（仅在 mode 为 vertical(垂直) 时可用） -->
            <!-- default-active  当前激活菜单的 index -->
            <!-- unique-opened  是否只保持一个子菜单的展开 -->
            <el-submenu v-for="v in menus" :index="v.id + ''" :key="v.id">
              <template #title>
                <i :class="menuIconMapForLevel1[v.id]"></i>
                <span>{{ v.authName }}</span>
              </template>
              <template #default>
                <el-menu-item
                  v-for="v2 in v.children"
                  :key="v2.id"
                  :index="v2.path"
                >
                  <!-- :index="/^(\/goods)\/+/.test(v2.path)?(v2.path==='/goods/add'?v2.path:(v2.path).substring(6)):(v2.path)" -->

                  <!-- <router-link :to="v2.path">
                    <i class="el-icon-menu"></i>
                    {{ v2.authName }}
                  </router-link> -->
                  <i class="el-icon-menu"></i>
                  {{ v2.authName }}
                </el-menu-item>
              </template>
            </el-submenu>
          </el-menu>
          <div class="collapse_toggle" @click="isCollapse = !isCollapse">
            <i
              :class="isCollapse ? 'el-icon-arrow-right' : 'el-icon-arrow-left'"
            ></i>
          </div>
        </el-aside>
        <el-container>
          <el-main>
            <router-view></router-view>
          </el-main>
          <el-footer>©2020 VueShop (香)-学习性-2017-0020</el-footer>
        </el-container>
      </el-container>
    </el-container>
  </div>
</template>

<script>
// @ is an alias to /src

export default {
  name: 'Home',
  data() {
    return {
      menus: [],
      menuIconMapForLevel1: {
        125: 'el-icon-s-custom',
        103: 'el-icon-lock',
        101: 'el-icon-goods',
        102: 'el-icon-s-order',
        145: 'el-icon-s-data',
      },
      isCollapse: false,
      // currentHash: '',
    };
  },
  methods: {
    logout() {
      localStorage.removeItem('token');
      this.$router.push('/login');
    },
    closeMenu() {
      this.$refs.menu.close('103');
      this.$refs.menu.close('125');
      this.$refs.menu.close('101');
      this.$refs.menu.close('102');
      this.$refs.menu.close('145');
    },
  },
  created() {
    // console.log(this.$route.path);
    this.__apis.getLeftMenus().then((data) => {
      if (data.data.meta.status === 200) {
        this.menus = data.data.data;
      }
    });
  },
  computed: {
    currentHash() {
      if (this.$route.path === '/goods/add') {
        return 'goods';
      }
      return this.$route.path.slice(1);
    },
  },
};
</script>

<style lang="less" scoped>
.home {
  a {
    text-decoration: none;
    color: #fff;
  }
  & > .el-container {
    height: 100vh;
    .el-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      background-color: #333744;
      div {
        display: flex;
        align-items: center;
        span {
          color: #fff;
          font-size: 22px;
          margin-left: 15px;
        }
      }
    }
    .el-aside {
      position: relative;
      background-color: #333744;
      .collapse_toggle {
        position: absolute;
        width: 100%;
        bottom: 0;
        cursor: pointer;
        color: #fff;
        display: flex;
        justify-content: center;
        align-items: center;
        height: 60px;
        &:hover {
          background-color: #292c36;
        }
        i {
          font-size: 24px;
        }
      }
    }
    .el-main {
      background-color: #eaedf1;
    }
    .el-footer {
      background-color: #333744;
      display: flex;
      color: #fff;
      justify-content: center;
      align-items: center;
    }
  }
  .el-menu {
    border: 0;
  }
}
</style>
